package cc.mrbird.system.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.common.util.DateUtil;
import cc.mrbird.system.dao.ProjectEntryMapper;
import cc.mrbird.system.domain.Menu;
import cc.mrbird.system.domain.ProjectEntry;
import cc.mrbird.system.service.ProjectEntryService;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("ProjectEntryService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectEntryServiceImpl extends BaseService<ProjectEntry> implements ProjectEntryService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProjectEntryMapper projectEntryMapper;


    @Override
    public List<ProjectEntry> findAllProjectEntry(ProjectEntry projectEntry, QueryRequest request) {
        try {
//            Example example = new Example(ProjectEntry.class);
//            Example.Criteria criteria = example.createCriteria();
//            if (StringUtils.isNotBlank(projectEntry.getCustomer())) {
//                criteria.andCondition("customer=", projectEntry.getCustomer());
//            }
//            if (StringUtils.isNotBlank(projectEntry.getMarketName())) {
//                criteria.andCondition("market_name=", projectEntry.getMarketName());
//            }
//            if (StringUtils.isNotBlank(projectEntry.getProjectNo())) {
//                criteria.andCondition("project_no=", projectEntry.getProjectNo());
//            }
//            example.setOrderByClause("project_id");

            return this.projectEntryMapper.findAllProjectEntry(projectEntry);

        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public ProjectEntry getProjectEntry(ProjectEntry projectEntry) {
        return this.projectEntryMapper.selectOne(projectEntry);
    }

    @Override
    public void addProjectEntry(ProjectEntry projectEntry) {
        projectEntry.setCreateTime(new Date());
        this.projectEntryMapper.insert(projectEntry);
    }

    @Override
    public void updateProjectEntry(ProjectEntry projectEntry) {
        projectEntry.setUpdateTime(new Date());
        this.projectEntryMapper.updateByPrimaryKey(projectEntry);
    }

    @Override
    public void deleteProjectEntry(String projectIds) {
        List<String> list = Arrays.asList(projectIds.split(","));
        this.batchDelete(list, "projectId", ProjectEntry.class);
    }

    @Override
    public List<ProjectEntry> importAllProjectEntry(MultipartFile importFactory) throws Exception {

        List<ProjectEntry> list = new ArrayList();
        InputStream inputStream = importFactory.getInputStream();
        XSSFWorkbook Workbook = new XSSFWorkbook(inputStream);

        XSSFSheet sheet = Workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
            XSSFRow row = sheet.getRow(rowNum);
            if (row!=null){
                ProjectEntry projectEntry = new ProjectEntry();
                //使用了getStringCellValue()方法来获取值，POI会判断单元格的类型，如果非字符串类型就会抛出上面的异常。
                //所以先使用setCellType()方法先将该单元格的类型设置为STRING
                //然后poi会根据字符串读取它
                if (StringUtils.isNotBlank(row.getCell(0).getStringCellValue())) {
                    row.getCell(0).setCellType(CellType.STRING);
                    projectEntry.setCustomer(row.getCell(0).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(1).getStringCellValue())) {
                    row.getCell(1).setCellType(CellType.STRING);
                    projectEntry.setMarketName(row.getCell(1).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(2).getStringCellValue())) {
                    row.getCell(2).setCellType(CellType.STRING);
                    projectEntry.setProjectNo(row.getCell(2).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(3).getStringCellValue())) {
                    row.getCell(3).setCellType(CellType.STRING);
                    projectEntry.setProjectBaseLine(row.getCell(3).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(4).getStringCellValue())) {
                    row.getCell(4).setCellType(CellType.STRING);
                    projectEntry.setProjectType(row.getCell(4).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(5).getStringCellValue())) {
                    row.getCell(5).setCellType(CellType.STRING);
                    projectEntry.setTypeName(row.getCell(5).getStringCellValue());
                }
                if (StringUtils.isNotBlank(row.getCell(6).getStringCellValue())) {
                    row.getCell(6).setCellType(CellType.STRING);
                    projectEntry.setMemorySize(row.getCell(6).getStringCellValue());
                }
                projectEntry.setCreateTime(new Date());
                XSSFCell cell = row.getCell(7);
                if (cell==null){
                    log.info("第"+rowNum+"行，第7列数据为null");
                }
                list.add(projectEntry);
            }
        }

        int i = this.projectEntryMapper.insertList(list);
        if (i>0) {
            return list;
        }
        return list;
    }
}
